﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.1f1
 *Date:           2019-03-26
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System;
using System.Text;

namespace IFramework
{
    public class ServerPackage : DataFrameInfo
    {
        private Encoding encoding = Encoding.UTF8;
        private const string acceptMask = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";//固定字符串
        private readonly string[] splitChars = null;
        public ServerPackage()
        {
            splitChars = new string[] { ": " };
        }
        public string RspAcceptPackage(AccessInfo access)
        {
            var accept = new AcceptInfo();
            accept.Connection = access.Connection;
            accept.Upgrade = access.Upgrade;
            accept.SecWebSocketLocation = access.Host;
            accept.SecWebSocketOrigin = access.Origin;
            accept.SecWebSocketAccept = (access.SecWebSocketKey + acceptMask).ToSha1Base64();
            return accept.ToString();
        }

        public byte[] GetBytes(string content)
        {
            var buf = encoding.GetBytes(content);
            Data = new BufferSegment()
            {
                Buffer = buf
            };
            Data.Buffer = encoding.GetBytes(content);
            DataLength = Data.Buffer.LongLength;
            return EncodingToBytes();
        }
        public byte[] GetBytes(OpCodeType code = OpCodeType.Bin)
        {
            OpCode = (byte)code;
            DataLength = Data.Buffer.LongLength;
            return EncodingToBytes();
        }
        public AccessInfo GetHandshakePackage(BufferSegment segOffset)
        {
            string msg = encoding.GetString(segOffset.Buffer, segOffset.Offset, segOffset.Len);
            string[] items = msg.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            if (items.Length < 6)
                throw new Exception("access format error..." + msg);
            AccessInfo access = new AccessInfo();
            access.HttpProto = items[0];
            foreach (var item in items)
            {
                string[] kv = item.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
                switch (kv[0])
                {
                    case "Connection":
                        access.Connection = kv[1];
                        break;
                    case "Host":
                        access.Host = kv[1];
                        break;
                    case "Origin":
                        access.Origin = kv[1];
                        break;
                    case "Upgrade":
                        access.Upgrade = kv[1];
                        break;
                    case "Sec-WebSocket-Key":
                        access.SecWebSocketKey = kv[1];
                        break;
                    case "Sec-WebSocket-Version":
                        access.SecWebSocketVersion = kv[1];
                        break;
                    case "Sec-WebSocket-Extensions":
                        access.SecWebSocketExtensions = kv[1];
                        break;
                }
            }
            return access;
        }
    }

}
